package com.automapper.sqlbuilder.impl;

import java.util.Map;

import com.automapper.query.CriteriaImpl;
import com.automapper.sqlbuilder.SqlBuilderTemplate;
import com.automapper.sqlfragment.SqlFragmentContext;
import com.automapper.utils.ParameterUtils;
import com.automapper.utils.PropertiesFacade;
/**
 * 级联查询sql以父表分页查询得出结果再已子查询方式关联子表
 * @author K
 *
 */
public class SelectAssociationPageByParentSqlBuilder extends SqlBuilderTemplate {	
	
	@Override
	public String buildSql(Object parameterObject, PropertiesFacade propertiesFacade) throws Exception {  
		StringBuilder sql = new StringBuilder();
		Map<String, Object> parameterMap = (Map<String, Object>) parameterObject;
		//获取传入参数
		CriteriaImpl criteria = (CriteriaImpl) ParameterUtils.getParameter(parameterMap);
		
		SqlFragmentContext sqlFragmentContext = criteria.getSqlFragmentContext();
		//拼装子查询sql
		String table = sqlFragmentContext.getTable();
		
		sql.append("select ")
		   .append(sqlFragmentContext.getColumnName())
		   .append(" from ")
		   .append(table)
		   .append(" where 1 = 1 ")
		   .append(sqlFragmentContext.getCondition());
		
		sql = sqlFragmentContext.genPage(sql);
		parameterMap.putAll(sqlFragmentContext.getParamMap());
		return sql.toString();
	}  
}
